<?php
defined('DSVH') or die('Restricted access');

if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
include('controller/c_thongke.php');     
include ('model/m_graphic.php');
$page = $_GET['act'];
 switch($page){
    case 'list':
    //Get language
    require('language/graphic/list.php');
    $heading_title = heading_title;
    $title = heading_title;
    $button_add = button_add;
    $button_edit = button_edit;
    $button_delete = button_delete;
    
  
    $tengraphic = ten_graphic;
    $thao_tac = thao_tac;
    $link_add ='index.php?module=graphic&act=add';
    
      if($_POST['selected']==""){
        $err['exist'] = chua_chon;
        $error_name = $err['exist'];
        }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
   
    // kiem tra bien pages
    if($_REQUEST['page']==""){ $page = 1;}
    else{$page = $_REQUEST['page'];}
    
    //get input for class page
      $limit = 5;
      $start = ($page - 1) * $limit; 
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid){
      foreach($_POST['selected'] as $id){
        $graphic = getgraphic($id);
         if($graphic['file']){
           unlink($graphic['file']);
         }
         delgraphic($id);
       }  
   
    //chuyen huong ve list
     header('LOCATION: '.$link_list);
     }
        
     $list_graphic = getListgraphic($start,$limit);
      
       $total = total();
       $paging = new Pagination();
       $paging->total = $total;
       $paging->limit = $limit;
       $paging->url = "index.php?module=graphic&act=list&page={page}";
       $paging->page = $page;
       $pagination = $paging->render();
     
    
    require('view/template/graphic/list.tpl');
    break;
	
	//case them chuc danh
	case 'add':
	require('language/graphic/add.php');
	$heading_title = heading_title;
 
    $tengraphic = ten_graphic;
    $motachitiet = mo_ta_chi_tiet;
    $motachung = mo_ta_chung;
    $hinhdaidien = hinh_dai_dien;
    $tag = tag;
    $list_loaigraphic = getListloaigraphic(0,100);
    $link_list = 'index.php?module=graphic&act=list';
    
    if($_POST['tieu_de']==""){
        $err['exist'] = chua_nhap_ten;
        $error_name = $err['exist'];
             }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
       
    insertgraphic($_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    
    }
    require('view/template/graphic/edit.tpl');
    break;
    
    //case sua dan toc
    case 'edit':
    require('language/graphic/edit.php');
    $heading_title = heading_title;
    $graphic = graphic;
	$tengraphic = ten_graphic;
    $motachitiet = mo_ta_chi_tiet;
    $motachung = mo_ta_chung;
    $hinhdaidien = hinh_dai_dien;
    $tag = tag;
    $link_list = 'index.php?module=graphic&act=list';
    $id = $_REQUEST['id'];
    $graphic = getgraphic($id);
    $list_loaigraphic = getListloaigraphic(0,100);
    //$_POST['file'] = $graphic['file'];
    
    
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
      
     editgraphic($id, $_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    
    }
        
    
    require('view/template/graphic/edit.tpl');
    break; 
    
    case 'del':
    get_file('language/graphic/del.php');
    $link_list = 'index?module=graphic&act=list';
    $id = $_REQUEST['id'];
    $graphic = getgraphic($id);
    if($graphic['avatar']){
           unlink('./data/image/graphic/'.$graphic['avatar']);
         }
    delgraphic($id);
     $_SESSION['success'] = del_success;
    //chuyen huong ve list
     header('LOCATION: '.$link_list);  
     exit();
         
    
    break;  
    
    
    case 'cat':
    //Get language
    require('language/graphic/list_cat.php');
    $heading_title = heading_title;
    $title = heading_title;
    $button_add = button_add;
    $button_edit = button_edit;
    $button_delete = button_delete;
    
  
    $tenloaigraphic = ten_loai_graphic;
    $thao_tac = thao_tac;
    $link_add ='index.php?module=graphic&act=addcat';
    
      if($_POST['selected']==""){
        $err['exist'] = chua_chon;
        $error_name = $err['exist'];
        }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
   
    // kiem tra bien pages
    if($_REQUEST['page']==""){ $page = 1;}
    else{$page = $_REQUEST['page'];}
    
    //get input for class page
      $limit = 5;
      $start = ($page - 1) * $limit; 
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid){
      foreach($_POST['selected'] as $id){
        $loaigraphic = getloaigraphic($id);
         delloaigraphic($id);
       }  
   
    //chuyen huong ve list
     header('LOCATION: '.$link_list);
     }
        
     $list_loaigraphic = getListloaigraphic($start,$limit);
      
     $total = totalloaigraphic();
     $paging = new Pagination();
     $paging->total = $total;
     $paging->limit = $limit;
     $paging->url = "index.php?module=graphic&act=cat&page={page}";
     $paging->page = $page;
     $pagination = $paging->render();
     
    
    require('view/template/graphic/list_cat.tpl');
    break;
    
     //case sua dan toc
    case 'editcat':
    require('language/graphic/edit_cat.php');
    $heading_title = heading_title;
    $tenloaigraphic = ten_loai_graphic;
    $tieude = tieu_de;
    $sapxep = sap_xep;
    $link_list = 'index.php?module=graphic&act=cat';
    $id = $_REQUEST['id'];
    
    $loaigraphic = getloaigraphic($id);
 
    //$_POST['file'] = $graphic['file'];
    
    
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
      
     editloaigraphic($id, $_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    
    }
    require('view/template/graphic/edit_cat.tpl');
    break; 
    
   	case 'addcat':
	require('language/graphic/add_cat.php');
	$heading_title = heading_title;
    $tieude = tieu_de;
    $sapxep = sap_xep;
	$tenloaigraphic = ten_loai_graphic;
  
    $link_list = 'index.php?module=graphic&act=cat';
    
    if($_POST['ten']==""){
        $err['exist'] = chua_nhap_ten;
        $error_name = $err['exist'];
             }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
       
    insertloaigraphic($_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    
    }
    require('view/template/graphic/edit_cat.tpl');
    break;
    
    
}
?>